package com.taichuan.selfcheck.ui.fragment;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.taichuan.selfcheck.R;
import com.taichuan.selfcheck.config.PropertyDefaultValue;
import com.taichuan.selfcheck.entity.SdCardInfo;
import com.taichuan.selfcheck.eventbus.UsbConnectStateEvent;
import com.taichuan.selfcheck.mvp.presenter.TestSdCardAndUsbPresenter;
import com.taichuan.selfcheck.mvp.viewinterface.TestSdCardAndUsbInterface;
import com.taichuan.selfcheck.ui.dialog.ResultDialog;
import com.taichuan.selfcheck.ui.fragment.base.AppBaseFragment;
import com.taichuan.tcutils.common.unit.FileSize;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;

/**
 * 该类主要用于
 *
 * @author CamelLuo
 * @version 2021/6/28
 */
public class TestSdCardAndUsbAndUsbFragment extends AppBaseFragment<TestSdCardAndUsbInterface, TestSdCardAndUsbPresenter> implements TestSdCardAndUsbInterface {

    @BindView(R.id.test_sd_card_state_imageView)
    ImageView stateIv;
    @BindView(R.id.test_sd_card_state_textView)
    TextView stateTv;
    @BindView(R.id.test_sd_card_path_textView)
    TextView pathTv;
    @BindView(R.id.test_sd_card_storage_info_textView)
    TextView storageInfoTv;
    @BindView(R.id.test_sd_card_info_layout)
    LinearLayout sdCardInfoLayout;
    @BindView(R.id.test_sd_card_type_textView)
    TextView sdCardTypeTv;
    @BindView(R.id.test_sd_card_uuid_textView)
    TextView sdCardUuidTv;
    @BindView(R.id.test_sd_card_name_textView)
    TextView sdCardNameTv;

    @BindView(R.id.test_usb_state_imageView)
    ImageView usbStateIv;
    @BindView(R.id.test_usb_state_textView)
    TextView usbStateTv;
    @BindView(R.id.test_usb_connected_textView)
    TextView usbConnectedTv;
    @BindView(R.id.test_usb_host_connected_textView)
    TextView usbHostConnectedTv;
    @BindView(R.id.test_usb_adb_mode_textView)
    TextView usbAdbModeTv;
    @BindView(R.id.test_usb_configured_textView)
    TextView usbConfiguredTv;
    @BindView(R.id.test_usb_info_layout)
    LinearLayout usbInfoLayout;

    ResultDialog resultDialog;

    @Override
    protected int setContentViewId() {
        return R.layout.fragment_test_sd_card;
    }

    @Override
    protected void initView(View view, Bundle bundle) {
        EventBus.getDefault().register(this);

        resultDialog = new ResultDialog(PropertyDefaultValue.FUNCTION_TEST_8, getResources().getString(R.string.sd_card_and_usb_test), getContext());            //设置弹窗发送的事件——灯光测试

        //检查设备当前是否插入SD卡
        mPresenter.checkAndRefreshSdCardState();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onXmlClick(View v) {
        super.onXmlClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (resultDialog.isShowing()) {
            resultDialog.dispatchKeyEvent(event);
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void refreshSdCardState(boolean isOpen) {
        if (isOpen) {
            stateIv.setSelected(true);
            stateTv.setText("已插入SD卡");
            stateTv.setTextColor(getResources().getColor(R.color.blue));
            sdCardInfoLayout.setVisibility(View.VISIBLE);
        } else {
            stateIv.setSelected(false);
            stateTv.setText("未插入SD卡");
            stateTv.setTextColor(getResources().getColor(R.color.red));
            sdCardInfoLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void refreshSdCardInfo(SdCardInfo sdCardInfo) {
        if (sdCardInfo != null) {
            sdCardUuidTv.setText(sdCardInfo.getUuid());
            sdCardNameTv.setText(sdCardInfo.getSdCardName());
            sdCardTypeTv.setText(sdCardInfo.getSdCardFileSystemType());
            pathTv.setText(sdCardInfo.getSdCardPath());
            storageInfoTv.setText(FileSize.formatByte(sdCardInfo.getAvailableSize(), FileSize.MB) + " MB / " +
                    FileSize.formatByte(sdCardInfo.getTotalSize(), FileSize.MB) + " MB");
        }
    }

    @Subscribe
    public void receiveUsbConnectStateEvent(UsbConnectStateEvent event) {
        if (event.isConnected()){
            usbInfoLayout.setVisibility(View.VISIBLE);
            usbStateTv.setText("已连接");
            usbStateTv.setTextColor(getResources().getColor(R.color.blue));
            usbStateIv.setSelected(true);
        }else {
            usbInfoLayout.setVisibility(View.GONE);
            usbStateTv.setText("未连接");
            usbStateTv.setTextColor(getResources().getColor(R.color.red));
            usbStateIv.setSelected(false);
        }

        usbConnectedTv.setText(String.format("%s", event.isConnected()));
        usbHostConnectedTv.setText(String.format("%s", event.isHostConnected()));
        usbAdbModeTv.setText(String.format("%s", event.isAdb()));
        usbConfiguredTv.setText(String.format("%s", event.isConfigured()));
    }

    @Override
    protected TestSdCardAndUsbPresenter createPresenter() {
        return new TestSdCardAndUsbPresenter(getActivity());
    }

    @Override
    protected boolean isAllowAutoFinishFragment() {
        return false;
    }

    @Override
    public void doOnAutoFinishFragment() {
        resultDialog.show();
    }

    @Override
    protected void getBundleValues(Bundle bundle) {

    }
}
